package com.jindianjichi.wx.controller;

import com.jindianjichi.wx.handler.WxChatMsgFactory;
import com.jindianjichi.wx.handler.WxChatMsgHandler;
import com.jindianjichi.wx.utils.MessageUtil;
import com.jindianjichi.wx.utils.SHA1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

@RestController
@Slf4j
public class CallbackController {
    private static final String token = "aibin1234";
    @Resource
    private WxChatMsgFactory wxChatMsgFactory;
    @RequestMapping("/test")
    public String test() {
        log.info("test");
        return "test";
    }


    /**
     //     * 回调消息校验
     //     */
    @GetMapping("callback")
    public String callback(@RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam("echostr") String echostr) {
        log.info("get验签请求参数：signature:{}，timestamp:{}，nonce:{}，echostr:{}",
                signature, timestamp, nonce, echostr);
        String shaStr = SHA1.getSHA1(token, timestamp, nonce, "");
        log.info("get验签结果：{}", shaStr);
                if (signature.equals(shaStr)) {
            return echostr;
        }
                return "unknown";


    }


    @PostMapping(value = "callback", produces = "application/xml;charset=UTF-8")
    public String callback(
            @RequestBody String requestBody,
                           @RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam(value = "msg_signature",required = false)String msg_signature) {
        log.info("接收到微信的请求：requestBody：{}, signature:{}，timestamp:{}，nonce:{}，echostr:{}",
                requestBody, signature, timestamp, nonce);
        Map<String, String> msgMap = MessageUtil.parseXml(requestBody);
        String toUserName = msgMap.get("ToUserName");
        String fromUserName = msgMap.get("FromUserName");
        String msgType = msgMap.get("MsgType");
        String event = msgMap.get("Event") == null ? "" : msgMap.get("Event");
        log.info("msgType:{},event:{}", msgType, event);
        StringBuilder sb=new StringBuilder();
        sb.append(msgType);
        if (!StringUtils.isEmpty(event)){
            sb.append(".");
            sb.append(event);
        }
        String msgTypeKey= sb.toString();

        WxChatMsgHandler handlerByMsgHandler = wxChatMsgFactory.getHandlerByMsgType(msgTypeKey);
        if (Objects.isNull(handlerByMsgHandler)){
            return "unknow";
        }
        String msg = handlerByMsgHandler.dealMsg(msgMap);


//        String msg="<xml>\n" +
//                "  <ToUserName><![CDATA["+fromUserName+"]]></ToUserName>\n" +
//                "  <FromUserName><![CDATA["+toUserName+"]]></FromUserName>\n" +
//                "  <CreateTime>1348831860</CreateTime>\n" +
//                "  <MsgType><![CDATA[text]]></MsgType>\n" +
//                "  <Content><![CDATA[你好，我是经典鸡翅，欢迎来学习]]></Content>\n" +
//                "</xml>";
        return msg;


    }

//    /**
//     * 回调消息校验
//     */
//    @GetMapping("callback")
//    public String callback(@RequestParam("signature") String signature,
//                           @RequestParam("timestamp") String timestamp,
//                           @RequestParam("nonce") String nonce,
//                           @RequestParam("echostr") String echostr) {
//        log.info("get验签请求参数：signature:{}，timestamp:{}，nonce:{}，echostr:{}",
//                signature, timestamp, nonce, echostr);
//        String shaStr = SHA1.getSHA1(token, timestamp, nonce, "");
//        if (signature.equals(shaStr)) {
//            return echostr;
//        }
//        return "unknown";
//    }

}
